<?php
namespace Home\Controller;
use Think\Controller;
class PinglunController extends Controller {
    private $admin_openid='oB_KewRQprjKA4ETYbqcRQgMiBk0';
    public function index(){
         $id=I('get.id');
        $type=I('get.type');
        session('t_orderid',$id);
        session('t_ordertype',$type);
    	// 设置选项，包括URL
    	$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . appid . "&redirect_uri=http://nuower.com/index.php/Pinglun/oauth&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
    	header("Location:".$url);
    }
    public function oauth() {
    	if (isset ( $_GET ['code'] )) {
    		$code = $_GET ['code'];
    		$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . appid . "&secret=" . appsecret . "&code=" . $code . "&grant_type=authorization_code";
    		$output = https_request ( $url );
    		// 第二步
    		if (isset ( $output )) {
    			$jsoninfo = json_decode ( $output, true );
                session(array('expire'=>$jsoninfo['expires_in']));
                session('access_token',$jsoninfo['access_token']);

    			$url_info = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $jsoninfo ['access_token'] . "&openid=" . appid . "&lang=zh_CN";
    			$userInfo = https_request ( $url_info );
    		} else {
    			$this->error ( "不好意思,出错了" );
    		}
    		// 第三步
    		if (isset ( $userInfo )) {
    			$res = json_decode ( $userInfo, true );
    			cookie( "openid", $res ['openid'] );
                cookie( "nickname", $res ['nickname'] );
    			cookie( "headimgurl", $res ['headimgurl'] );
    			$this->pinglun();
    		} else {
    			$this->error ( "不好意思,出错了" );
    		}
    	} else {
    		$this->error ( "不好意思,出错了" );
    	}
    }

    public function pinglun()
    {  
         $id=session('t_orderid');
        $type=session('t_ordertype');
          if(!IS_POST){
        switch ($type) {
        case 'carry':
        $M=M('orders');
            break;
        case 'nocarry':
         $M=M('order');
            break;
        case 'pro':
          $M=M('order_p');
            break;
        
        default:
           $M=M('orders');
            break;
    }   
        $orderinfo=$M->find($id);
        $this->assign('orderinfo',$orderinfo);
        $this->display('pinglun');


          }else{
            $data=$_POST;
            if (empty($type)) {
               $this->error('评论失效',U('Order/pl_list'));
            }
            switch ($type) {
        case 'carry':
        $M=M('orders');
            break;
        case 'nocarry':
         $M=M('order');
            break;
        case 'pro':
          $M=M('order_p');
            break;
        
        default:
            # code...
            break;
    }   

            $pl['id']=$data['id'];
            $pl['headimgurl']=$_COOKIE['headimgurl'];
            $pl['user_nickname']=$_COOKIE['nickname'];
            $pl['pl_desc']=$data['comment'];
            $pl['score']=$data['score'];
            $pl['pinglunstatus']=1;
            $pl['pl_addtime']=time();
             $map['id']=$pl['id'];
            $map['pinglunstatus']=1;
            $f=$M->where($map)->find();
            if ($f) {
               $this->error('已评论',U('Order/pl_list')); 
            }else{
              $r= $M->save($pl);  
            }

           if ($r) {
               $this->success('评论成功',U('Order/pl_list'));
           }

          }
    }





public function pl_list()
{
   $this->display();
}
        


    
}